#include <bits/stdc++.h>

using namespace std;


// 比较运算符优先级
int precedence(char op) {
    if (op == '+' || op == '-')
        return 1;
    if (op == '*' || op == '/')
        return 2;
    return 0;
}

// 中缀转后缀
std::string infixToPostfix(std::string infix) {
    std::stack<char> stack;
    std::string postfix = "";

    for (char c: infix) {
        if (c == ' ')
            continue;
        if (isalpha(c)) {

            postfix += c;
            postfix += " ";
        } else if (c == '(') {
            stack.push(c);
        } else if (c == ')') {
            while (!stack.empty() && stack.top() != '(') {
                postfix += stack.top();
                postfix += " ";
                stack.pop();
            }
            if (!stack.empty() && stack.top() == '(')
                stack.pop();
        } else {
            while (!stack.empty() && precedence(stack.top()) >= precedence(c)) {
                postfix += stack.top();
                postfix += " ";
                stack.pop();
            }
            stack.push(c);
        }
    }

    while (!stack.empty()) {
        postfix += stack.top();
        postfix += " ";
        stack.pop();
    }

    return postfix;
}

int main() {
    map<char, int> VARS;
    for (string line; getline(cin, line);) {
        if (line == "%%%")break;
        for (int i = 0; i < line.length(); i++) {
            if (line[i] == '=') {
                string var = line.substr(0, i);
                istringstream iss(line.substr(i + 1));
                int val;
                iss >> val;
                VARS[var[0]] = val;
                break;
            }
        }
    }

    for (string line; getline(cin, line);) {
        string postfix = infixToPostfix(line);
        istringstream iss(postfix);
        stack<int> STACK;
        for (string tok; iss >> tok;) {
            istringstream iss2(tok);
            if (VARS.find(tok[0])!= VARS.end()) {
                STACK.push(VARS[tok[0]]);
            } else {
                int b = STACK.top();
                STACK.pop();
                int a = STACK.top();
                STACK.pop();
                if (tok[0] == '+') {
                    STACK.push(a + b);
                } else if (tok[0] == '-') {
                    STACK.push(a - b);
                } else if (tok[0] == '*') {
                    STACK.push(a * b);
                } else if (tok[0] == '/') {
                    STACK.push(a / b);
                }

            }

        }
        cout << STACK.top() << endl;
    }
}

/**************************************************************
    Problem: 1189
    User: acm
    Language: C++
    Result: 正确
    Time:3 ms
    Memory:2280 kb
    Judger:judge.aliyun2
****************************************************************/